package com.hzcx.app.simplechat.ui.setting;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.main.MainActivity;
import com.hzcx.app.simplechat.ui.publicui.UpdateVersionDialog;
import com.hzcx.app.simplechat.ui.publicui.WebViewActivity;
import com.hzcx.app.simplechat.ui.publicui.bean.ProtocolBean;
import com.hzcx.app.simplechat.ui.publicui.bean.UpdateVersionBean;
import com.hzcx.app.simplechat.ui.setting.contract.AboutContract;
import com.hzcx.app.simplechat.ui.setting.presenter.AboutPresenter;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.down.DownLoadUtil;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 关于
 */
public class AboutActivity extends BaseActivity<AboutPresenter> implements AboutContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_version_name)
    TextView tvVersionName;
    @BindView(R.id.tv_app_name)
    TextView tvAppName;
    @BindView(R.id.tv_protocol_user)
    TextView tvProtocolUser;
    @BindView(R.id.tv_protocol_private)
    TextView tvProtocolPrivate;

    @Override
    public int getLayout() {
        return R.layout.activity_about;
    }

    @Override
    public void initPresenter() {
        mPresenter = new AboutPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("关于" + getResources().getString(R.string.app_name));

        tvVersionName.setText(AppHelper.getVersionName(this));

        tvAppName.setText(getResources().getString(R.string.app_name) + "聊天");
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.tv_protocol_user, R.id.tv_protocol_private, R.id.tv_update_version})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_protocol_user: // 用户协议
                mPresenter.getProtocol(this, "agreement");
                break;

            case R.id.tv_protocol_private: // 隐私政策
                mPresenter.getProtocol(this, "policy");
                break;

            case R.id.tv_update_version: // 检查更新
//                Beta.checkUpgrade(true, false);
                mPresenter.updateVersion(this);
                break;
        }
    }

    /**
     * 协议内容获取成功
     *
     * @param data
     */
    @Override
    public void protocolResult(ProtocolBean data) {
        startActivity(new Intent(this, WebViewActivity.class)
                .putExtra(WebViewActivity.INTENT_TITLE, data.getName())
                .putExtra(WebViewActivity.INTENT_WEB_URL, data.getContenturl()));
    }

    @Override
    public void updateVersionResult(UpdateVersionBean data) {
        try {
            if (data == null || EmptyUtils.isEmpty(data.getDownload_link())) {
                showTips("提示", "您已经是最新版本", null);
                return;
            }

            UpdateVersionDialog versionDialog = new UpdateVersionDialog(this, data, url -> {
//                startActivity(new Intent(AboutActivity.this, WebViewActivity.class)
//                        .putExtra(WebViewActivity.INTENT_WEB_URL, url));

                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            });
            versionDialog.show();

//            UpdateVersionDialog versionDialog = new UpdateVersionDialog(this, data, url ->
//                    DownLoadUtil.downLoadNewVersion(AboutActivity.this, url));
//            versionDialog.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
